{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第3章 理解什么是写代码与Python的基本类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3-1 什么是代码，什么是写代码\n",
    "\n",
    "+ 什么是代码：代码是现实世界在计算机世界中的映射  \n",
    "  那么问题来了，他满足离散数学中的什么样的映射关系，有什么映射要求吗？  \n",
    "  因为一个现实的事物可以用多种计算机语言表示，且一种计算机中的事物可以代表多种现实世界的事物。他们可以任意形式的映射。\n",
    "+ 那什么是写代码呢：写代码往往是解决现实中的一些问题  \n",
    "  写代码是利用代码将现实中的事物用计算机语言描述，来解决生活中的问题。\n",
    "\n",
    "就像一句话：来源于生活，又高于生活  \n",
    "就好比我的世界中的石英、矿石，有了它们才能创造世界。它们是最基础的物质，这就是基本数据类型  \n",
    "在基本数据类型上我们加以一定的构造，就构成了另外的一些数据类型，又可以理解为类的基础  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3-2 Python的基本数据类型\n",
    "\n",
    "+ Number: 数字类型\n",
    "  + 整数：int  \n",
    "    其他语言：short, int, long\n",
    "  + 浮点数：float  \n",
    "    其他语言：单精度(float), 双精度(double)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'float'>\n",
      "<class 'int'>\n",
      "<class 'float'>\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "print(type(1/2)) # 正常除法\n",
    "print(type(1//2)) # 整除\n",
    "print(type(1+0.1)) # 整型 + 浮点型 = 浮点型\n",
    "print(type(1*1.0)) # 整型 * 浮点型 = 浮点型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3-3~3.4 进制表示及转换\n",
    "\n",
    "### 一、各进制的表示与转换\n",
    "\n",
    "+ python中二进制表示：在数字前面加0b，比如0b10\n",
    "+ python中八进制表示：在数字前面0o，比如0o10、0o11\n",
    "+ python中十六进制表示：0x1F、0x2F\n",
    "+ python中十进制表示：11,12,1，没有特殊符号\n",
    "\n",
    "### 二、转换方法\n",
    "\n",
    "+ bin()\t把其他进制的数转换为二进制\n",
    "+ int()\t把其他进制数转换为十进制\n",
    "+ hex()\t把其他进制转换为十六进制\n",
    "+ oct()\t把其他进制转换为八进制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b1010\n",
      "0b111\n",
      "0b1110\n"
     ]
    }
   ],
   "source": [
    "print(bin(10)) # 十进制-->二进制\n",
    "print(bin(0o7)) # 八进制-->二进制\n",
    "print(bin(0xE)) # 十六进制-->二进制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "0x378\n",
      "0o7\n",
      "0o3567\n"
     ]
    }
   ],
   "source": [
    "print(int(0b111)) # 二进制-->十进制\n",
    "print(hex(888)) # 十进制-->十六进制\n",
    "print(oct(0b111)) # 二进制-->八进制\n",
    "print(oct(0x777)) # 十六进制-->八进制"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3-5 布尔类型和复数\n",
    "\n",
    "python的基本数据类型：\n",
    "\n",
    "+ Number:数字\n",
    "+ bool: 表 真，假\n",
    "+ complex:复数，数学基础。\n",
    "\n",
    "bool类型在pathon中的特性，代码举例如下：\n",
    "\n",
    "```python\n",
    "type(True) --> class 'bool'\n",
    "int(True) - >1\n",
    "int(False)->0\n",
    "bool(1)-->True\n",
    "bool(0)-->False\n",
    "bool(2)-->True\n",
    "bool(2.2)-->True\n",
    "bool(-1.1)-->True\n",
    "```\n",
    "\n",
    "非0表示真，0表示假\n",
    "\n",
    "```python\n",
    "bool(0b01) - >True\n",
    "bool(0b0)->False\n",
    "bool('abc')==True\n",
    "bool('')==False\n",
    "bool([1,2,3])==True\n",
    "bool([])==False\n",
    "bool({1,2,3})==True\n",
    "bool({})==False\n",
    "bool(None)==False #空值会被认为False\n",
    "```\n",
    "\n",
    "并不是只有数字可以和bool类型之间转换，很多类型都可以和bool类型之间转换："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.6~3.7 字符串:单引号、双引号、三引号\n",
    "\n",
    "+ 单引号，双引号，三引号包裹表达式，都为字符串，三引号支持多行.   # 引号都为英文状态 \n",
    "+ 引号中表达式内部存在引号，会导致报错，例：'let's go' , 解决方法，将外部的引号改为双引号或者三引号\n",
    "  + `'Let's go'`  报错\n",
    "  + `''Let's go''`  外部双引号，内部单引号有效   （推荐这种方式，更美观，更自然）\n",
    "  + `'''Let's go'''` 外部三引号，内部单引号或者双引号有效(也可以是三个双引号)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3-8 转义字符\n",
    "\n",
    "**无法“看见”的字符**，如换行字符在显示时看不见;**与语言本身语法有冲突的字符**，如单引号在python语法中表示字符串，但也可作为普通字符\n",
    "\n",
    "+ `\\n`换行\n",
    "+ `\\r`回车(光标在下一行)\n",
    "+ `\\'`单引号\n",
    "+ `\\t`横向制表符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3-9 原始字符串\n",
    "\n",
    "用前缀r可以屏蔽转义字符的影响\n",
    "\n",
    "r 用法\n",
    "例如：输出文件路径\n",
    "\n",
    "```python\n",
    "print('c:\\nrthwind\\northwest')\n",
    "```\n",
    "\n",
    "结果为：\n",
    "\n",
    "```shell\n",
    "c:\n",
    "rthwind\n",
    "orthwest\n",
    "```\n",
    "\n",
    "这个结果并不是我们想要的,解决方法如下：\n",
    "+ 1.加入\\转义字符：\n",
    "\n",
    "```python\n",
    "print('c:\\\\nrthwind\\\\northwest')\n",
    "```\n",
    "\n",
    "+ 2.字符串前加r (不是一个普通字符串，而是一个原始字符串,r或者R，大写小写都可以)\n",
    "\n",
    "```python\n",
    "print(r'c:\\nrthwind\\northwest')\n",
    "```\n",
    "\n",
    "上面两个方法可以正常输出 `c:\\nrthwind\\northwest`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3-10~3-12 字符串运算\n",
    "\n",
    "### 字符串拼接\n",
    "\n",
    "```python\n",
    "\"hello\"+\"world\"='helloworld'\n",
    "\"hello\"*3='hellohellohello'\n",
    "```\n",
    "\n",
    "### 字符提取\n",
    "\n",
    "```python\n",
    "\"hello world\"[0]='h'\n",
    "\"hello world\"[3]='l'\n",
    "\"hello world\"[4]='o'\n",
    "\"hello world\"[-1]='d'\n",
    "\"hello world\"[-3]='r'\n",
    "\"hello world\"[6] --> w\n",
    "\"hello world\"[-5] -->w\n",
    "```\n",
    "\n",
    "### 字符串截取\n",
    "\n",
    "> `str[n:m]`  ,截取下标n到m之间的字符串，负数表示从尾部向前数，左开右闭，即不含位置m处的字符\n",
    "\n",
    "```python\n",
    "\"hello world\"[0:4] -->hell\n",
    "\"hello world\"[0:5] -->hello ,取下标0到5之间的字符串，不含5，所以是hello\n",
    "\"hello world\"[0:-1] --> hello worl, 取0到倒第1个字符之间的字符串，不含最后一个\n",
    "\"hello world\"[0:-3] --> hello wo\n",
    "'hello world'[4:5] --> o\n",
    "```\n",
    "\n",
    "+ `[a:b]` 两个都是正数的情况 b要在需要截取的最后一个字符的序号推一位  记住 需要从0开始数 \n",
    "  > `\"hello world\"[6:11]`  结果为 `'world'`, 如果b的输入的数字大于字符串总体的长度的话，它会按最大的最后的位置给你取，即11为其他大于11的数时 输出照旧\n",
    "+ `[b:]` 从第b行 一直截取到字符串的末尾\n",
    "+ `[-b:]` 从这个字符串的末尾到着数b个字符并且把它截取出来.即倒数第b个字符到末尾位置之间的字符串\n",
    "+ `[:b]` 从字符串下标为0的位置开始截取到下标为b的字符串，不含下标为b的字符\n",
    "\n",
    "```python\n",
    "'hello world'[1:] = 'ello world'\n",
    "'hello world'[-1:] = 'd'\n",
    "'hello world'[:2] = 'he'\n",
    "'hello world'[:-2] = 'hello wor'\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
